package com.ycode.bang.anbang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.utils.StringUtils;
import com.ycode.bang.anbang.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 编辑地址/个人报修
 */
public class EditAddress2Activity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.lin_infor)
    LinearLayout lin_Infor;//编辑的地址
    @InjectView(R.id.Sw_switch)
    Switch mSwitch;//代申报切换按钮
    @InjectView(R.id.lin_Selectxiaoqu)
    LinearLayout lin_Select;//选择小区
    @InjectView(R.id.et_name)
    EditText mEtName;//姓名
    @InjectView(R.id.xiaoqu)
    TextView xiaoqu;//小区
    @InjectView(R.id.et_number)
    EditText mEtNumber;//电话
    @InjectView(R.id.btn_ok)
    Button mBt_Ok;//

    @InjectView(R.id.lin_Selectzhuanghao)
    LinearLayout lin_SelectZH;//选择幢号
    @InjectView(R.id.zhuanghao)
    TextView zhuanghao;//幢号
    @InjectView(R.id.lin_type)
    LinearLayout lin_type;//楼宇类别
    @InjectView(R.id.type)
    TextView type;//楼宇类别
    @InjectView(R.id.lin_Selectdanyuan)
    LinearLayout lin_SelectDY;//选择单元
    @InjectView(R.id.danyuan)
    TextView danyuan;//单元
    @InjectView(R.id.lin_Selectfangjian)
    LinearLayout lin_SelectFJ;//选择房间
    @InjectView(R.id.fangjian)
    TextView fangjian;//房间
    private String plotid = "";
    private String value = "";//house——id
    private String zhuangId = "";//幢号的id
    Bundle bundle = new Bundle();
    private String type1 = "";//楼宇类别
    private String type2 = "";//楼宇类别

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_address2);
        ButterKnife.inject(this);
        initView();
    }

    public void initView() {
        title.setText("编辑地址");
        mSwitch.setOnClickListener(this);
        lin_Select.setOnClickListener(this);
        lin_SelectZH.setOnClickListener(this);
        lin_SelectDY.setOnClickListener(this);
        lin_SelectFJ.setOnClickListener(this);
        mBt_Ok.setOnClickListener(this);
        lin_type.setOnClickListener(this);
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    lin_Infor.setVisibility(View.VISIBLE);
                } else {
                    lin_Infor.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_Selectxiaoqu:
                Intent intent = new Intent(this, SelectPlotActivity.class);
//                Bundle bundle = new Bundle();
                bundle.putString("plot", "");
                bundle.putString("plotid", "");
                intent.putExtras(bundle);
                startActivityForResult(intent, 2016);
                break;
            case R.id.btn_ok:
                Intent intent2 = new Intent();
                intent2.setClass(this, CreatePublicActivity.class);
                Bundle bundle2 = new Bundle();
                if (TextUtils.isEmpty(xiaoqu.getText().toString().trim())) {
                    ToastUtil.show(this, "请选择小区");
                    return;
                }
                if (TextUtils.isEmpty(zhuanghao.getText().toString().trim())) {
                    ToastUtil.show(this, "请选择幢号");
                    return;
                }
                if (TextUtils.isEmpty(fangjian.getText().toString().trim()) && !type1.equals("独栋别墅")) {
                    ToastUtil.show(this, "请选择房间号");
                    return;
                }
                if (TextUtils.isEmpty(mEtName.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(mEtNumber.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入手机号码");
                    return;
                }
                if (!StringUtils.isPhone(mEtNumber.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入正确的手机号码");
                    return;
                }
                if (!TextUtils.isEmpty(danyuan.getText().toString())) {
                    bundle2.putString("plot", xiaoqu.getText().toString().trim() + zhuanghao.getText() + danyuan.getText() + fangjian.getText());
                } else {
                    bundle2.putString("plot", xiaoqu.getText().toString().trim() + zhuanghao.getText() + fangjian.getText());
                }

                bundle2.putString("number", mEtNumber.getText().toString().trim());
                bundle2.putString("name", mEtName.getText().toString().trim());
                bundle2.putString("plotid", plotid);
                bundle2.putString("value", value);
                intent2.putExtra("bundle2", bundle2);
                setResult(Activity.RESULT_OK, intent2);
                finish();
                break;
            case R.id.lin_type:
                if (TextUtils.isEmpty(xiaoqu.getText().toString().trim())) {
                    ToastUtil.show(this, "请选选择小区！");
                    break;
                }
                Intent intent7 = new Intent(this, SelectBuildingTypeActivity.class);
                bundle.putString("plotid", plotid);
                bundle.putString("zhuangId", zhuangId);
                intent7.putExtras(bundle);
                startActivityForResult(intent7, 2010);
                break;
            case R.id.lin_Selectzhuanghao:
                if (TextUtils.isEmpty(xiaoqu.getText().toString().trim())) {
                    ToastUtil.show(this, "请选选择小区！");
                    break;
                }
                Intent intent3 = new Intent(this, SelectZhuangActivity.class);
                bundle.putString("plotid", plotid);
                bundle.putString("zhuangId", zhuangId);
                bundle.putString("type", type2);
                intent3.putExtras(bundle);
                startActivityForResult(intent3, 2017);
                break;
            case R.id.lin_Selectdanyuan:
                if (TextUtils.isEmpty(zhuanghao.getText().toString().trim())) {
                    ToastUtil.show(this, "请选选择幢号！");
                    break;
                }
                Intent intent4 = new Intent(this, SelectDanYuanActivity.class);
                bundle.putString("plotid", plotid);
                bundle.putString("zhuangId", zhuangId);
                intent4.putExtras(bundle);
                startActivityForResult(intent4, 2018);
                break;
            case R.id.lin_Selectfangjian:
//                if (TextUtils.isEmpty(fangjian.getText().toString().trim())) {
//                    ToastUtil.show(this, "请选选择单元号！");
//                    break;
//                }
                if (TextUtils.isEmpty(zhuanghao.getText().toString().trim())) {
                    ToastUtil.show(this, "请选选择幢号！");
                    break;
                }
                Intent intent5 = new Intent(this, SelectFangJianActivity.class);
                bundle.putString("plotid", plotid);
                bundle.putString("value", value);//house id
                bundle.putString("zhuangId", zhuangId);
                intent5.putExtras(bundle);
                startActivityForResult(intent5, 2019);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case 2016:
                    Bundle bundle = data.getBundleExtra("bundle");
                    xiaoqu.setText(bundle.getString("plot"));
                    plotid = bundle.getString("plotid");
//                    value=bundle.getString("value");
                    break;
                case 2017:
                    Bundle bundle2 = data.getBundleExtra("bundle");
                    zhuanghao.setText(bundle2.getString("plot"));
                    zhuangId = bundle2.getString("zhuangId");
                    break;
                case 2018:
                    danyuan.setText(data.getStringExtra("plot"));
                    break;
                case 2019:
                    fangjian.setText(data.getStringExtra("plot"));
                    value = data.getStringExtra("value");
                    break;
                case 2010:
                    Bundle bundle3 = data.getBundleExtra("bundle");
                    type.setText(bundle3.getString("plot"));
                    type1 = bundle3.getString("plot");
                    type2 = bundle3.getString("zhuangId");
                    if (type1.equals("独栋别墅")) {
                        lin_SelectDY.setVisibility(View.GONE);
                        lin_SelectFJ.setVisibility(View.GONE);
                    } else {
                        lin_SelectDY.setVisibility(View.VISIBLE);
                        lin_SelectFJ.setVisibility(View.VISIBLE);
                    }
                    zhuanghao.setText("");
                    danyuan.setText("");
                    fangjian.setText("");
//                    zhuangId = bundle3.getString("zhuangId");
                    break;
            }
        }
    }
}
